import http from '@/utils/http'
export const getDepartment = () => {
  return async (dispatch) => {
    const res = await http.get('/company/department')

    const getTree = (list, root) => {
      const arr = []
      list.forEach((item) => {
        if (item.pid === root) {
          const children = getTree(list, item.id)
          if (children.length) {
            item.children = children
          }
          arr.push(item)
        }
      })
      return arr
    }
    dispatch({
      type: 'department/getDepartment',
      payload: getTree(res.data.data.depts, ''),
    })
  }
}
